package com.group1.bookmanagement.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 验证码工具类
 */
public class IdentityUtils {

	// 字符
	public static final char[] chars = "1234567890QWERTYUIOPASDFGHJKLZXCVBNM".toCharArray();
	public static Random random = new Random();

	/**
	 * 将图片转换成字节流并返回
	 * @param code
	 * @return
	 */
	public static ByteArrayInputStream getImageAsInputStream(String code) {
		BufferedImage image = getImage(code);
		return convertImageToStream(image);
	}

	/**
	 * 获取验证码图片
	 * @param code
	 * @return
	 */
	public static BufferedImage getImage(String code) {
		int width = 100; // 图片宽度
		int height = 30; // 图片高度

		Color color = getRandomColor();
		Color reverse = getReverseColor(color);

		// 生成带字符串的文本图片
		// 创建图片缓存区 传参为宽高和图片类型
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		// 获取画笔并绘画
		Graphics g = image.getGraphics();
		// 设置字体
		g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
		// 设置画笔颜色
		g.setColor(color);
		// 绘制背景
		g.fillRect(0, 0, width, height);
		// 设置画笔颜色
		g.setColor(reverse);
		// 绘制字符
		g.drawString(code, 18, 20);
		// 设置最多100个噪音点
		for (int i = 0, n = random.nextInt(100); i < n; i++) {
			g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
		}
		// 3.输出图片
		// try {
		// ImageIO.write(image, "JPG", resp.getOutputStream());
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

		return image;
	}

	/**
	 * 获取4位随机数
	 * 
	 * @return
	 */
	public static String getRandomString() {
		StringBuffer buffer = new StringBuffer();
		int index;// 获取随机chars下标
		for (int i = 0; i < 4; i++) {
			index = random.nextInt(chars.length);// 获取随机chars下标
			buffer.append(chars[index]);
		}
		return buffer.toString();
	}

	/**
	 * 随机颜色
	 * 
	 * @return
	 */
	public static Color getRandomColor() {
		return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
	}

	/**
	 * 返回某颜色的反色
	 * 
	 * @param c
	 * @return
	 */
	public static Color getReverseColor(Color c) {
		// 防止反色和原色相近
		if (c.getRed() < 130 && c.getRed() > 125 && c.getBlue() < 130 && c.getBlue() > 125 && c.getGreen() < 130
				&& c.getGreen() > 125) {
			return new Color(255, 255, 255);
		} else {
			return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c.getBlue());
		}
	}

	/**
	 * 图片转化为流
	 * 
	 * @param image
	 * @return
	 */
	private static ByteArrayInputStream convertImageToStream(BufferedImage image) {

		ByteArrayInputStream inputStream = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(bos);
		try {
			jpeg.encode(image);
			byte[] bts = bos.toByteArray();
			inputStream = new ByteArrayInputStream(bts);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return inputStream;
	}

}
